package com.ruoyi.web.controller.store;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.store.domain.*;
import com.ruoyi.store.service.IMemberService;
import com.ruoyi.store.service.IStuPaperService;
import com.ruoyi.store.service.IStuQuestionStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/store/paper")
public class StuPaperController extends BaseController {
    @Resource
    private IStuPaperService stuPaperService;
    @Resource
    private IStuQuestionStoreService stuQuestionStoreService;
    @Resource
    private IMemberService memberService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('store:paper:list')")
    public TableDataInfo list(StuPaper stuPaper)
    {
        startPage();
        List<StuPaper> list = stuPaperService.selectStuPaperList(stuPaper);
        return getDataTable(list);
    }

    //获取试卷题目详情页
    @GetMapping("/paperQuestionList")
    public AjaxResult selectPaperQuestionByPaperList(Long paperId){
        startPage();
        return  AjaxResult.success(stuPaperService.selectStuPaperQuesByPaperList(paperId));
    }

    @GetMapping("/paperDetail")
    public AjaxResult selectPaperById(Long paperId){
        startPage();
        return  AjaxResult.success(stuPaperService.selectStuPaperByPaperId(paperId));
    }

    @PostMapping("/addUserAnswer")
    public AjaxResult addPaperQuestion(@RequestBody StuHisExam stuHisExams)
    {
        stuHisExams.setUserId(getUserId());
        stuHisExams.setCreateBy(getUsername());
        System.out.println(stuHisExams);
        List<StuUserAnswer> optionList = stuHisExams.getOptionList();
        for (StuUserAnswer stuUserAnswer:optionList){
            System.out.println(stuUserAnswer);
        }
        return AjaxResult.success(stuPaperService.insertHisExam(stuHisExams));
    }



    @PostMapping
    public AjaxResult add(@RequestBody StuPaper stuPaper){
        if (UserConstants.NOT_UNIQUE.equals(stuPaperService.checkPaperNameUnique(stuPaper)))
        {
            return AjaxResult.error("新增试卷'" + stuPaper.getPaperName() + "'失败，试卷已存在");
        }
        stuPaper.setCreateBy(getUsername());
        return toAjax(stuPaperService.insertPaper(stuPaper));
    }


    @GetMapping(value = { "/", "/{paperId}" })
    public AjaxResult getInfo(@PathVariable(value = "paperId", required = false) Long paperId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("stores",stuQuestionStoreService.selectAllStores());

//        if (StringUtils.isNotNull(paperId))
//        {
//
////            StuQuestion stuQuestion = stuQuestionService.selectQuesById(quesId);
////            ajax.put(AjaxResult.DATA_TAG, stuQuestion);
////            ajax.put("storeIds", stuQuestionStoreService.selectStuStoresListByQuesId(quesId));
//        }
        return ajax;
    }


    @GetMapping(value = {"counts/{storeId}"})
    public AjaxResult getPaperCounts(@PathVariable(value = "storeId") Long storeId){
        Long storeCounts = stuQuestionStoreService.selectStoreCounts(storeId);

        return AjaxResult.success(storeCounts);
    }

    @DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[]paperIds){
        return  toAjax(stuPaperService.delStuPapersByIds(paperIds));
    }



}
